package com.zx.system.block.controller.system;

import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.system.block.param.MenuParam;
import com.zx.system.block.service.ISysMenuService;
import com.zx.system.block.vo.RouterVO;
import com.zx.system.block.vo.SysMenuVO;
import com.zx.system.block.vo.TreeVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.StatusUpdate;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * description: 菜单 <br>
 * create: 2024-09-07 18:03
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class SysMenuController {
    private final ISysMenuService iSysMenuService;

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public ListWrapper<RouterVO> getRouters() {
        ListWrapper<RouterVO> result = new ListWrapper<>();
        List<RouterVO> routerVoList = iSysMenuService.selectMenuTreeByUserId();
        result.setData(routerVoList);
        return result;
    }

    /**
     * 获取菜单树
     *
     * @param menuParam 菜单参数
     * @return {@link ListWrapper< TreeVO >}
     * @author zhou  xun
     * @since 2024-12-25
     */
    @OperateLog(value = "获取菜单树")
    @GetMapping("getMenuTree")
    public ListWrapper<TreeVO> getMenuTree(MenuParam menuParam) {
        return iSysMenuService.getMenuTree(menuParam);
    }

    // 以下为菜单管理接口
    @PreAuthorize("hasAuthority('system:menu:list')")
    @OperateLog(value = "获取菜单列表")
    @GetMapping("/list")
    public ListWrapper<SysMenuVO> list(MenuParam menuParam) {
        return iSysMenuService.selectMenuVOList(menuParam);
    }

    /**
     * 新增菜单
     *
     * @param menuParam 菜单参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-12-21
     */
    @PreAuthorize("hasAuthority('system:menu:add')")
    @OperateLog(value = "新增菜单")
    @PostMapping("/addMenu")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "菜单参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addMenu(@RequestBody MenuParam menuParam) {
        return iSysMenuService.addMenu(menuParam);
    }

    /**
     * 编辑菜单
     *
     * @param menuParam 菜单参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-12-21
     */
    @PreAuthorize("hasAuthority('system:menu:edit')")
    @OperateLog(value = "编辑菜单")
    @PutMapping("/editMenu")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "菜单参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editMenu(@RequestBody MenuParam menuParam) {
        return iSysMenuService.editMenu(menuParam);
    }

    /**
     * 修改菜单状态
     *
     * @param menuParam 菜单参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-07
     */
    @PreAuthorize("hasAuthority('system:menu:edit')")
    @OperateLog(value = "修改菜单状态")
    @PutMapping("/editMenuStatus")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "菜单参数", rules = {ValidateRulesEnum.OBJECT}, groups = {StatusUpdate.class})
    )
    public ResponseWrapper editMenuStatus(@RequestBody MenuParam menuParam) {
        return iSysMenuService.editMenuStatus(menuParam.getId(), menuParam.getStatus());
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-12-21
     */
    @PreAuthorize("hasAuthority('system:menu:remove')")
    @OperateLog(value = "删除菜单")
    @DeleteMapping("/deleteMenu/{menuId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "菜单ID", rules = {ValidateRulesEnum.NOT_NULL}, groups = {Update.class})
    })
    public ResponseWrapper deleteMenu(@PathVariable("menuId") String menuId) {
        return iSysMenuService.deleteMenu(menuId);
    }
}